// Author: wsfuyibing <682805@qq.com>
// Date: 2025-05-15

package list

import (
	"context"
	"gitee.com/go-libs/db-xorm/db"
	"gitee.com/go-libs/result"
	"gitee.com/go-wares/framework-iris/framework/src/logics"
	"github.com/kataras/iris/v12"
	"server/app/errs"
	"server/app/http/models"
	"server/app/lib"
)

// Logic
// 轮播图列表.
type Logic struct {
	admin  *models.ArcadeAdmins
	arcade *models.Arcades

	response []*Response
}

// New
// 创建逻辑实例.
func New() logics.Logical {
	return &Logic{
		response: make([]*Response, 0),
	}
}

// Before
// 前置校验.
func (o *Logic) Before(_ context.Context, i iris.Context) (err error) {
	o.arcade, o.admin = lib.Arcade.GetMaster().GetArcade(i)
	return
}

// Run
// 执行逻辑.
func (o *Logic) Run(ctx context.Context, _ iris.Context) result.Result {
	var (
		err  error
		res  = result.New()
		list []*models.ArcadeSwiper
		sess *db.Session
	)

	if sess, err = db.Config.GetSlave(ctx); err != nil {
		return res.WithError(errs.ErrDatabaseQuery)
	}

	if err = sess.Where("arcade_id = ?", o.arcade.Id).OrderBy(`enabled ASC, sort ASC`).Find(&list); err != nil {
		return res.WithError(errs.ErrDatabaseQuery)
	}

	for _, item := range list {
		o.response = append(o.response, (&Response{}).With(item))
	}

	return res.WithList(o.response)
}
